/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admintool.imp03_data;

import java.util.ArrayList;

import admintool.AdminToolView;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 *
 * @author Administrator
 */
public class IMP03_Message {
    
    private class Message{

        private int messageID = -1;
        private int candidateID = -1;
        private String subject = "";
        private String content = "";
        
        public Message(int messageID, int candidateID, String subject, String content ) {
            this.messageID = messageID;
            this.candidateID = candidateID;
            this.subject = subject;
            this.content = content;
        }

        public int getCandidateID() {
            return candidateID;
        }

        /**
         * @return the messageID
         */
        public int getMessageID() {
                return messageID;
        }

        /**
         * @return the subject
         */
        public String getSubject() {
                return subject;
        }

        /**
         * @return the content
         */
        public String getContent() {
                return content;
        }
        

    }
    
    private static ArrayList<IMP03_Message> messages = new ArrayList<IMP03_Message>();
    private Message message = null;
    
    /**
     * 
     * @param messageID
     * @param candidateID
     * @param name
     * @param subject
     * @param content
     */
    public IMP03_Message(int messageID, int candidateID, String subject, String content) {
        this.message = new Message(messageID, candidateID, subject, content);
    }
    
    /**
     * 
     * @param messageID
     * @param candidate
     * @param name
     * @param subject
     * @param content
     */
    public IMP03_Message(int messageID, IMP03_Candidate candidate, String subject, String content) {
        this.message = new Message(messageID, candidate.getCandidateID(), subject, content);
    }

    /**
     * 
     * @return
     */
    public static IMP03_Message getEmptyMessageObject(){
        return new IMP03_Message(-1, -1, AdminToolView.EMPTY, AdminToolView.EMPTY);
    }

    /**
     * 
     * @param messageID 
     * @return
     */
    public static IMP03_Message getSpecificMessageFromDB(int messageID){
        return messages.get(messageID);
    }
    
    /**
     * 
     * @param reload
     * @return
     */
    public static ArrayList<IMP03_Message> getAllAvailableMessagesFromDB(boolean reload){
        
        PreparedStatement ps = null;
        ResultSet rs = null;
        
        try {
            if(reload == true || messages.isEmpty()){
                messages.clear();
                ps = AdminToolView.getDBConnection().prepareStatement("SELECT MessageID, CandidateID, Subject, Content FROM message");
                rs = ps.executeQuery();
                while(rs.next()){
                    messages.add(new IMP03_Message(rs.getInt(1), rs.getInt(2), rs.getString(3), rs.getString(4)));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(rs != null){
                    rs.close();
                }
                if(ps != null){
                    ps.close();
                }
            } catch (Exception e) {
            }
        }
        
        return messages;
    }
    

    @Override
    public String toString() {
        return getMessageCandidateName();
    }
    
    /**
     * 
     * @return
     */
    public String getMessageSubject() {
    	return message.getSubject();
    } 
    
    /**
     * 
     * @return
     */
    public String getMessageContent() {
    	return message.getContent();
    }
    
    /**
     * 
     * @return
     */
    public int getMessageID() {
    	return message.getMessageID();
    }    
    
    public int getMessageCandidateID(){
        return message.getCandidateID();
    }

    public String getMessageCandidateName(){
        IMP03_Candidate candidate = IMP03_Candidate.getSpecificCandidateFromDB(message.getCandidateID());
        String returnValue = AdminToolView.EMPTY;
        if (candidate != null){
            returnValue = candidate.getLastName() + ", " + candidate.getFirstName();
        }
        return returnValue;
    }
  
}
